//
//  LoginVerView.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2020/4/22.
//  Copyright © 2020 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

class LoginVerView: NSView {
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        self.addSubview(loginView)
        loginView.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalTo(80)
            make.center.equalToSuperview()
        }
        
        loginView.addSubview(anoView)
        anoView.snp.makeConstraints { (make) in
            make.width.top.centerX.equalToSuperview()
            make.height.equalTo(40)
        }
        
        let line = NSView()
        line.wantsLayer = true
        line.layer?.backgroundColor = color_gray(200).cgColor
        loginView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.height.equalTo(1)
            make.width.equalToSuperview()
            make.center.equalToSuperview()
        }
        
        loginView.addSubview(sendBtn)
        sendBtn.snp.makeConstraints { (make) in
            make.width.equalTo(90)
            make.right.equalToSuperview()
            make.top.equalTo(anoView.snp.bottom).offset(10)
            make.height.equalTo(20)
        }
       
        loginView.addSubview(pwdView)
        pwdView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalTo(sendBtn.snp.left).offset(-5)
            make.height.equalTo(40)
            make.top.equalTo(anoView.snp.bottom).offset(1)
        }
        
        
        
    }
    
    lazy var loginView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.borderWidth = 1
        view.layer?.borderColor = color_gray(200).cgColor
        view.layer?.cornerRadius = 5
        view.layer?.masksToBounds = true
        return view
    }()
    lazy var anoView: IconTEF = {
        let view = IconTEF()
        view.icon.image = NSImage.imageNamed("icon_login_ano")
        view.zhTEF.textField.placeholderString = "请输入手机号".localized()
        return view
    }()
    
    lazy var pwdView: IconTEF = {
        let view = IconTEF()
        view.icon.image = NSImage.imageNamed("icon_login_pwd")
        view.zhTEF.textField.placeholderString = "请输入验证码".localized()
        return view
    }()
    
    lazy var sendBtn: ZHButton = {
        let btn = ZHButton.init(frame: NSRect.zero)
        btn.zh_title = "发送验证码".localized()
        btn.zh_font = NSFont.systemFont(ofSize: 12)
        btn.zh_color = color_main140
        btn.zh_backgroundColor = NSColor.clear
        btn.target = self
        btn.action = #selector(sendBtnAction(senber:))
        return btn
    }()
    
    @objc func sendBtnAction(senber:ZHButton)  {
        let tefText = self.anoView.zhTEF.textField.stringValue
        let text = tefText.clearId()
        if String.validatedPhone(phoneStr: text) == true {
            //发送请求
            NetEngine.reponse_verify(phone: Int(text)!, success: nil)
            
            dispatchTimer(timeInterval: 1, repeatCount: 10) { (time, count) in
                senber.isEnabled = false
                senber.zh_title = "重新发送".localized()+"("+("\(count)")+")"
                if count==0 {
                    senber.isEnabled = true
                    senber.zh_title = "发送验证码".localized()
                }
            }
        }else {
            show_msg("输入格式有误".localized())
        }
    }
    
}

